﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Aletfa.LightManager.Board
{
    /// <summary>
    /// Interaction logic for UnitPieceControl.xaml
    /// </summary>
    public partial class UnitPieceControl : UserControl
    {
        public UnitPieceControl()
        {
            InitializeComponent();
        }

        public Unit DataSource
        {
            get { return this.Tag as Unit; }
            set
            {
                this.Tag = value;
                this.imageEvil.Visibility = (value.IsEnemy) ? Visibility.Visible : System.Windows.Visibility.Collapsed;
                this.imageUser.Visibility = (!value.IsEnemy) ? Visibility.Visible : System.Windows.Visibility.Collapsed;
                this.border.Fill = (value.IsEnemy) ? GraphicUtilities.BrushFromHtml("#FA5858") : GraphicUtilities.BrushFromHtml("#A9D0F5");
                this.name.Content = value.Name;
            }
        }

        public void SelectionAnimation(bool start)
        {
            ThicknessAnimation ta = null;
            if (start)
            {
                ta = new ThicknessAnimation();
                ta.AutoReverse = true;
                ta.RepeatBehavior = RepeatBehavior.Forever;
                ta.Duration = new Duration(TimeSpan.FromMilliseconds(300));
                ta.From = new Thickness(0, 5, 0, 0);
                ta.To = new Thickness(0, -15, 0, 0);
            }

            this.imageEvil.BeginAnimation(Grid.MarginProperty, ta);
            this.imageUser.BeginAnimation(Grid.MarginProperty, ta);
        }

        public void HealAnimation(bool start, int ammount)
        {
            ThicknessAnimation ta1 = null;
            ThicknessAnimation ta2 = null;
            this.imgHealing.Visibility = this.imgHealing2.Visibility = Visibility.Hidden;
            if (start)
            {
                ta1 = new ThicknessAnimation();
                ta1.RepeatBehavior = (ammount <= 0) ? RepeatBehavior.Forever : new RepeatBehavior(ammount);
                ta1.Completed += (sender, e) => animation_Completed(sender, e, this.imgHealing);
                ta1.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
                ta1.From = new Thickness(40, 70, 0, 0);
                ta1.To = new Thickness(40, -70, 0, 0);

                ta2 = new ThicknessAnimation();
                ta2.RepeatBehavior = (ammount <= 0) ? RepeatBehavior.Forever : new RepeatBehavior(ammount);
                ta2.Completed += (sender, e) => animation_Completed(sender, e, this.imgHealing2);
                ta2.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
                ta2.From = new Thickness(32, 80, 0, 0);
                ta2.To = new Thickness(32, -60, 0, 0);
                this.imgHealing.Visibility = this.imgHealing2.Visibility = Visibility.Visible;
            }

            this.imgHealing.BeginAnimation(Grid.MarginProperty, ta1);
            this.imgHealing2.BeginAnimation(Grid.MarginProperty, ta2);
        }

        public void LoseLifeAnimation(bool start, int ammount)
        {
            ThicknessAnimation ta1 = null;
            ThicknessAnimation ta2 = null;
            this.imgHeart.Visibility = this.lblLifeLost.Visibility = Visibility.Hidden;
            if (start)
            {
                ta1 = new ThicknessAnimation();
                ta1.RepeatBehavior = (ammount <= 0) ? RepeatBehavior.Forever : new RepeatBehavior(ammount);
                ta1.Completed += (sender, e) => animation_Completed(sender, e, this.imgHeart);
                ta1.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
                ta1.From = new Thickness(20, 70, 0, 0);
                ta1.To = new Thickness(20, -70, 0, 0);

                ta2 = new ThicknessAnimation();
                ta2.Completed += (sender, e) => animation_Completed(sender, e, this.lblLifeLost);
                ta2.RepeatBehavior = (ammount <= 0) ? RepeatBehavior.Forever : new RepeatBehavior(ammount);
                ta2.Duration = new Duration(TimeSpan.FromMilliseconds(2000));
                ta2.From = new Thickness(35, 70, 0, 0);
                ta2.To = new Thickness(35, -72, 0, 0);
            }

            this.imgHeart.Visibility = this.lblLifeLost.Visibility = Visibility.Visible;
            this.imgHeart.BeginAnimation(Grid.MarginProperty, ta1);
            this.lblLifeLost.BeginAnimation(Grid.MarginProperty, ta2);
        }

        void animation_Completed(object sender, EventArgs e, UIElement element)
        {
            element.Visibility = System.Windows.Visibility.Hidden;
        }
    }
}
